package cn.util;

import java.io.File;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.catalina.connector.Request;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
import org.springframework.http.HttpRequest;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.StringUtils;


public class PDFJS {
	//private static final Log LOG = LogFactory.getLog(PDFJS.class);
	 
    /**
     * @param inputFilePath
     *            源文件路径，如："e:/test.docx"
     * @return
     */
    public static File openOfficeToPDF(String inputFilePath,String ReceivePath) {  //1
        System.out.println("进入转换");
    	return office2pdf(inputFilePath,ReceivePath);
    }
 
    /**
     * 根据操作系统的名称，获取OpenOffice.org 4的安装目录<br>
     * @return OpenOffice.org 4的安装目录    */
      //这里需要你自己改，你自己的openoffice.org4 的安装目录
     public static String getOfficeHome() {
        String osName = System.getProperty("os.name");
        System.out.println("操作系统名称:" + osName);
        if (Pattern.matches("Linux.*", osName)) {
            return "/opt/openoffice.org4";
        } else if (Pattern.matches("Windows.*", osName)) {
            return "C:/Program Files (x86)/OpenOffice 4";
        } else if (Pattern.matches("Mac.*", osName)) {
            return "/Applications/OpenOffice.org.app/Contents/";
        }
        return null;
    }
 
    /**
     * 连接OpenOffice.org 并且启动OpenOffice.org
     *
     * @return
     */
    public static OfficeManager getOfficeManager() {
        DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
        // 设置OpenOffice.org 4的安装目录
        config.setOfficeHome(getOfficeHome());
        // 启动OpenOffice的服务
        OfficeManager officeManager = config.buildOfficeManager();
        officeManager.start();
        return officeManager;
    }
 
    /**
     * 转换文件
     *
     * @param inputFile
     * @param outputFilePath_end
     * @param inputFilePath
     * @param
     * @param converter
     */
    public static File converterFile(File inputFile, String outputFilePath_end, String inputFilePath,
                                     OfficeDocumentConverter converter) {  //4
        File outputFile = new File(outputFilePath_end);
        // 假如目标路径不存在,则新建该路径
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().mkdirs();
        }
        converter.convert(inputFile, outputFile);
        System.out.println("文件：" + inputFilePath + "转换为目标文件：" + outputFile + "成功!");
        return outputFile;
    }
 
    /**
     *目标文件路径，如："e:/test_docx.pdf"
     * @param inputFilePath
     *            源文件路径，如："e:/test.docx"
     * @param
     * @return
     */
    
    public static File office2pdf(String inputFilePath,String ReceivePath) {  //2
        OfficeManager officeManager = null;
        try {
            if (StringUtils.isEmpty(inputFilePath)) {
                //LOG.info("输入文件地址为空，转换终止!");
            	System.out.println("输入文件地址为空，转换终止!");
                return null;
            }
 
            File inputFile = new File(inputFilePath);
            // 转换后的文件路径
            String name=inputFilePath.substring(inputFilePath.lastIndexOf("\\")+1);
            String outputFilePath_end = getOutputFilePath(ReceivePath+"\\"+name); //3
 
            if (!inputFile.exists()) {
                //LOG.info("输入文件不存在，转换终止!");
            	System.out.println("输入文件不存在，转换终止!");
                return null;
            }
 
            // 获取OpenOffice的安装路劲
            officeManager = getOfficeManager();
            // 连接OpenOffice
            OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
 
            return converterFile(inputFile, outputFilePath_end, inputFilePath, converter);
        } catch (Exception e) {
            //LOG.error("转化出错!", e);
        	System.out.println("转化出错!");
        	e.printStackTrace();
        	
        } finally {
            // 停止openOffice
            if (officeManager != null) {
                officeManager.stop();
            }
        }
        return null;
    }
 
    /**
     * 获取输出文件
     * @param inputFilePath
     * @return
     */
    public static String getOutputFilePath(String inputFilePath) {  //3
        String outputFilePath = inputFilePath.replaceAll("." + getPostfix(inputFilePath), ".pdf");
        return outputFilePath;
    }
 
    /**
     * 获取inputFilePath的后缀名，如："e:/test.pptx"的后缀名为："pptx"<br>
     *
     * @param inputFilePath
     * @return
     */
    public static String getPostfix(String inputFilePath) {
        return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1);
    }

	
 
}
